/*
配置示例:
[
	['setTitle', [`§l坐标`]],
    ['setContent', [content(pl)]],
	["addButton", ["按钮1"], callback],
]

*/
class Form{
	constructor(mode, buildConfig){
		this.mode = mode;
		this.form = undefined;	
		this.buildConfig = undefined;
		this.indexs = [];

		this.buildConfig = buildConfig;
		if (this.mode == 'newCustomForm'){
			this.form = mc.newCustomForm();
		}else if (this.mode == 'newSimpleForm'){
			this.form = mc.newSimpleForm();
		}
		let index = -1;
		for (let entry of buildConfig){
			index++;
			let func = entry[0];
			let args = entry[1];
			let note = entry[2];
			switch (func){
				case 'setTitle':
					this.form.setTitle(args[0]);continue;
				case 'setContent':
					this.form.setContent(args[0]);continue;
				case 'addButton':
					this.form.addButton(...args);break;
				case 'addLabel':
					this.form.addLabel(...args);break;
				case 'addInput':
					this.form.addInput(...args);break;
				case 'addSwitch':
					this.form.addSwitch(...args);break;
				case 'addDropdown':
					this.form.addDropdown(...args);break;
				case 'addSlider':
					this.form.addSlider(...args);break;
				case 'stepSlider':
					this.form.addStepSlider(...args);break;
				case 'commit': 
					continue;
				case 'close': 
					continue;
				default:
					logger.error(`表单配置错误, 无效的 ${func}`);break;
			}
			if (this.mode == 'newCustomForm'){
				this.indexs.push(note);
			}else if (this.mode == 'newSimpleForm'){
				this.indexs.push(index);
			}
			
		}
	}

	async sendForm(pl){
		if (this.mode == 'newCustomForm'){
			pl.sendForm(this.form, (pl, args)=>{
				if (args == null){return this.buildConfig.pop().pop()(pl);}
				let res = {};
				for (let i = 0; i < this.indexs.length; i++){
					res[this.indexs[i]] = args[i];
				}
				this.buildConfig[this.buildConfig.length-2].pop()(pl, res);
				// for (let i = 0; i < this.indexs.length; i++){
				// 	this.buildConfig[this.indexs[i]].pop()(pl, args[i]);
				// }
			});
		}else if (this.mode == 'newSimpleForm'){
			pl.sendForm(this.form, (pl, id)=>{
				if (id == null){return this.buildConfig.pop().pop()(pl);}
				this.buildConfig[this.indexs[id]].pop()(pl, id);
			});
		}
	}
}

// 结束菜单
function menu_end(pl, title, endmsg, lastForm, lastForm_run){
    var menu_end = mc.newSimpleForm();
    menu_end.setTitle(title);
    menu_end.setContent('\n\n'+endmsg+'\n\n\n\n');
    menu_end.addButton('§6§l关闭表单');
    if (lastForm != undefined){
        menu_end.addButton('§l返回上一页');
    }
    
    pl.sendForm(menu_end,function(pl,id){
        if (id == null){return;}
        if (id == 1){
			if (typeof lastForm == "function"){
				return lastForm(pl);
			}else{
				return pl.sendForm(lastForm,lastForm_run);
			}
        }
    });
}

// 页面表单
function menu_page(pl, queryResList, buttonStyle, callBack, returnBack){
	queryListPart(pl, queryResList, 1 ,returnBack);
	function queryListPart(pl, queryResList, page ,returnBack){
		let querResListPart = queryResList.slice((page-1)*20, (page-1)*20+20);
		let fm_queryListPart = mc.newSimpleForm();
		fm_queryListPart.setTitle(`§l请选择一项`);
		fm_queryListPart.setContent(`§a当前页: §r${page}§7/${Math.ceil(queryResList.length/20)}`);
		fm_queryListPart.addButton(`§l上一页`);
		fm_queryListPart.addButton(`§l跳转到...`);
		for (let i = 0; i < querResListPart.length; i++){ //${(page-1)*20+i+1}
			fm_queryListPart.addButton(buttonStyle(querResListPart[i]));
		}
		fm_queryListPart.addButton(`§l跳转到...`);
		fm_queryListPart.addButton(`§l下一页`);
		pl.sendForm(fm_queryListPart, (pl, id)=>{
			if (id == null){return returnBack(pl);}
			if (id == 0){
				if (page == 1){
					return queryListPart(pl, queryResList, Math.ceil(queryResList.length/20) ,returnBack);
				}
				return queryListPart(pl, queryResList, page-1 ,returnBack);
			}
			if (id == querResListPart.length + 3){
				if (page+1 > Math.ceil(queryResList.length/20)){
					return queryListPart(pl, queryResList, 1 ,returnBack);
				}
				return queryListPart(pl, queryResList, page+1 ,returnBack);
			}
			if (id == 1 || id == querResListPart.length + 2){return selectPage(pl, queryResList, page, returnBack);}
			return callBack(pl, querResListPart[id-2], returnBack);
		});
	}

	function selectPage(pl, queryResList, page, returnBack){
		let fm_selectPage = mc.newCustomForm()
		.setTitle(`§l跳转到...`)
		.addLabel(`\n `)
		.addInput(`输入要跳转的页数`, `范围: 1-${Math.ceil(queryResList.length/20)}`, `${page}`);
		pl.sendForm(fm_selectPage, (pl, data)=>{
			if (data == null){return queryListPart(pl, queryResList, page ,returnBack);}
			if (!(/(^[1-9]\d*$)/.test(data[1]))){
				return menu_end(pl, `§l跳转到...`, `§cERROR 请输入正整数`, (pl)=>{return queryListPart(pl, queryResList, page ,returnBack);});
			}
			let selectPage_num = Number(data[1]);
			if (selectPage_num>Math.ceil(queryResList.length/20)){
				return menu_end(pl, `§l跳转到...`, `§cERROR 超出页码范围`, (pl)=>{return queryListPart(pl, queryResList, page ,returnBack);});
			}
			return queryListPart(pl, queryResList, selectPage_num ,returnBack);
		});
	}
}

export {
	Form,
	menu_end,
	menu_page
}; 